<template>
  <el-form :model="form" ref="formRef" :rules="rules" label-position="top">
    <el-row justify="space-between">
      <el-col :span="24">
        <el-form-item label="电影封面" required prop="movieImage">
          <ZzjUpload v-model:img="form.movieImage"></ZzjUpload>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="电影名称" required prop="movieName">
          <el-input v-model="form.movieName"/>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="导演" prop="movieDirector">
          <el-select
              v-model="form.movieDirector"
              multiple
              filterable
              allow-create
              default-first-option
              :reserve-keyword="false"
              style="width: 100%;"
              placeholder="请输入主演姓名"></el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="主演" prop="movieActor">
          <el-select
              v-model="form.movieActor"
              multiple
              filterable
              allow-create
              default-first-option
              :reserve-keyword="false"
              style="width: 100%;"
              placeholder="请输入主演姓名">
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="上映时间" required prop="movieShowTime">
          <el-date-picker
              v-model="form.movieShowTime"
              type="date"
              placeholder="2022-06-03"
          />
        </el-form-item>
      </el-col>
      <el-col :span="10">
        <el-form-item label="时长/分" required prop="movieDuration">
          <el-input v-model="form.movieDuration" placeholder="120"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="简介描述" prop="movieDescription">
          <el-input
              v-model="form.movieDescription"
              :rows="2"
              type="textarea"
              placeholder="请说点什么..."
          />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-button type="primary" style="width: 100%;" @click="clickSubmit">提交</el-button>
      </el-col>
    </el-row>
  </el-form>

</template>

<script setup lang="ts">
import {onBeforeMount, ref} from "vue";
import ZzjUpload from "../../../components/ZzjUpload.vue";
import api from "../../../api/api";
import {ElMessage} from "element-plus";

const props = defineProps({
  movieInfo: {
    type: Object,
    default: undefined
  }
})
const form = ref()
const formRef = ref()
const rules = ref({
  movieImage: [
    {required: true, message: '请输入电影名称', trigger: 'blur'},
  ],
  movieName: [
    {required: true, message: '请输入电影名称', trigger: 'blur'},
  ],
  movieDirector: [
    {required: true, message: '请输入导演名', trigger: 'blur'},
  ],
  movieActor: [
    {required: true, message: '请输入主演', trigger: 'blur'},
  ],
  movieShowTime: [
    {required: true, message: '请输入上映时间', trigger: 'blur'},
  ],
  movieDuration: [
    {required: true, message: '请输入电影时长', trigger: 'blur'},
  ],
})

onBeforeMount(() => {
  //创建
  if (Object.is(props.movieInfo, undefined)) {
    form.value =
        {
          movieName: '',
          movieDirector: '',
          movieActor: '',
          movieImage: '',
          movieDescription: '',
          movieDuration: '',
          movieShowTime: '',
        }
  } else {
    //编辑
    form.value = props.movieInfo
  }
})


const clickSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      if (props.movieInfo == undefined) {
        api.movie.createMovie(form.value).then(res => {
          if (res.data.code == 0) {
            ElMessage.success("添加成功")
          }
        })
      } else {
        api.movie.updateMovie({
          movie: form.value
        }).then(res => {
          if (res.data.code == 0) {
            ElMessage.success("修改成功")
          }
        })
      }
    } else {

    }
  })
}
</script>


<style scoped>

</style>